package com.compass.controller;

import com.compass.entity.BaseProtocolDo;
import com.compass.entity.TestCaseDo;
import com.compass.mapper.TestCaseMapper;
import com.compass.service.TestCaseService;
import com.compass.util.CopyProperties;
import com.compass.vo.ResponseMessage;
import com.compass.vo.TablePage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA.
 * User: li
 * Date: 2019/4/21
 * Time: 9:49
 * PROJECT：compass
 * PACKAGE: com.compass
 *
 * @author:55324936@qq.com To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/testCase")
@SessionAttributes(names = {"interfaceId", "interfaceType"})
public class TestCaseController {

    @Autowired
    TestCaseService testCaseService;
    @Autowired
    TestCaseMapper testCaseMapper;

    @RequestMapping("/testCasePage")
    public ModelAndView page(Integer interfaceId, String interfaceType, Model model) {
        ModelAndView modelAndView = new ModelAndView();
        if (!StringUtils.isEmpty(interfaceType) && null != interfaceId) {
            model.addAttribute("interfaceId", interfaceId);
            model.addAttribute("interfaceType", interfaceType);
        }
        ;
        modelAndView.setViewName("testcase/testCasePage");
        return modelAndView;
    }

    @RequestMapping("/testCaseList")
    public @ResponseBody
    Object testCaseList(TestCaseDo testCaseDo) {
        return testCaseService.getTestCaseList(testCaseDo).getObj();
    }

    @RequestMapping("/modifyTestCasePage")
    public ModelAndView modifyTestCasePage(Integer id,HttpSession httpSession){
        ModelAndView modelAndView=new ModelAndView();
        String interfaceType = (String) httpSession.getAttribute("interfaceType");
        String viewName = "/testcase/" + interfaceType + "TestCaseForm";
        TestCaseDo testCaseDo =testCaseMapper.getTestCaseById(id);
        if(null !=testCaseDo){
            CopyProperties.copyToModel(testCaseDo,modelAndView);
        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }

    @RequestMapping("/addTestCasePage")
    public ModelAndView addTestCasePage(HttpSession httpSession) {
        ModelAndView modelAndView = new ModelAndView();
        String interfaceType = (String) httpSession.getAttribute("interfaceType");
        Integer interfaceId = (Integer) httpSession.getAttribute("interfaceId");
        String viewName = "/testcase/" + interfaceType + "TestCaseForm";
        String paramModel = testCaseService.getParamModel(interfaceId, interfaceType);
        modelAndView.addObject("paramModel", paramModel);
//        BaseProtocolDo baseProtocolDo = componentService.getComponentById(httpServletRequest);
//        if(null!= baseProtocolDo){
//            CopyProperties.copyToModel(baseProtocolDo,modelAndView);
//        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }

    @RequestMapping("/saveTestCase")
    public ModelAndView saveTestCase(TestCaseDo testCaseDo) {
        ModelAndView modelAndView = new ModelAndView();
        ResponseMessage responseObj = testCaseService.addTestCase(testCaseDo);
//        if (!responseObj.isSuccess()) {
//            modelAndView.addObject("msg", responseObj.getMsg());
//            modelAndView.setViewName("/login/registerPage");
//            return modelAndView;
//        }
        modelAndView.setViewName("redirect:/testCase/testCasePage");
        return modelAndView;
    }

}
